﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace DreamCMS.WebCore.Common
{
    public class ConfigHelper
    {
        #region 静态构造函数，清空并初始化ConfigHelper的字段
        /// <summary>
        /// 初始化ConfigHelper中的字段
        /// </summary>
        static ConfigHelper()
        {
            ClearCache();
        }
        #endregion

        #region 获取站点自定义前缀
        /// <summary>
        /// 站点Cache，Session，Cookie的自定义前缀，读取WebConfig来初始化他
        /// </summary>
        private static string sitePrefix = null;
        /// <summary>
        /// WebConfig中的站点的全局前缀配置。
        /// 如果字段有值就直接返回字段的值
        /// </summary>
        public static string SitePrefix
        {
            get
            {
                if (string.IsNullOrEmpty(ConfigHelper.sitePrefix))
                {
                    ConfigHelper.sitePrefix = GetAppValueByKey("sitePrefix");
                }
                return ConfigHelper.sitePrefix;
            }
        }

        #endregion

        #region 获取站点相对根路径
        private static string siteRootPath = null;
        /// <summary>
        /// 获取站点根路径。只读属性。站点根路径。（一个相对于域名的路径，而不是物理路径）
        /// </summary>
        public static string SiteRootPath
        {
            get
            {
                if (string.IsNullOrEmpty(ConfigHelper.siteRootPath))
                {
                    ConfigHelper.siteRootPath = ConfigHelper.GetAppValueByKey("siteRootPath");
                }
                return ConfigHelper.siteRootPath;
            }
        }

        #endregion

        #region 获取数据库路径
        private static string accessDbRelativePath = null;
        /// <summary>
        /// 从WebConfig中的AppSetting节点获取设置的Access数据库相对站点的路径.（已经转换为全物理路径）
        /// 转为物理路径返回
        /// </summary>
        public static string AccessDbPhysicPath
        {
            get
            {
                if (string.IsNullOrEmpty(ConfigHelper.accessDbRelativePath))
                {
                    ConfigHelper.accessDbRelativePath = GetAppValueByKey("accessDbRelativePath");
                }
                string PhysicDbPath = System.Web.HttpContext.Current.Server.MapPath(ConfigHelper.SiteRootPath + ConfigHelper.accessDbRelativePath);
                return PhysicDbPath;
            }
        }
        public static string AccessDbRelativePath
        {
            get
            {
                if (string.IsNullOrEmpty(ConfigHelper.accessDbRelativePath))
                {
                    ConfigHelper.accessDbRelativePath = GetAppValueByKey("accessDbPath");
                }
                return accessDbRelativePath;
            }
        }

        #endregion

        #region 清空和初始化ConfigHelper缓存
        /// <summary>
        /// 清空ConfigHelper的字段缓存，即重新从WebConfig获取一遍新的字段值。
        /// 清空缓存可以解决改了WebConfig但数据没变化的问题
        /// </summary>
        public static void ClearCache()
        {
            ConfigHelper.sitePrefix = GetAppValueByKey("sitePrefix");
            ConfigHelper.siteRootPath = GetAppValueByKey("sitePath");
            ConfigHelper.accessDbRelativePath = GetAppValueByKey("accessDbRelativePath");
        }
        #endregion

        #region 内部方法--获取AppSetting节点值
        /// <summary>
        /// 根据用户传入的key获取WebConfig里面AppSetting节点的某个值。
        /// </summary>
        /// <param name="key">AppSetting节点的索引值</param>
        /// <returns>节点的值</returns>
        private static string GetAppValueByKey(string key)
        {
            return System.Configuration.ConfigurationManager.AppSettings[key];
        }

        #endregion
    }
}
